myApp.onPageInit('announcement-list', function (page) {
    console.log("onPageInit " + page.name);

    var list = $$(page.container).find('.list-block'),
        empty = $$(page.container).find('.empty-view'),
        ul = list.find('ul'),
        refreshContainer = $$(page.container).find('.pull-to-refresh-content');
    list.data('request', null);
    list.data('lastpage', false);
    list.data('nextpage', 0);

    function loadPage() {
        var refresing = list.data('nextpage') == 0,
            req = true;
        $$.getJSONP(SERVER_PATH + 'getAnnouncementList.do', {num: PAGE_SIZE, start: (list.data('nextpage') * PAGE_SIZE)}, function (json) {

            if (json.code == 1) {
                list.data('nextpage', list.data('nextpage') + 1);

                var selectItem;
                if (refresing) {
                    selectItem = list.find('ul').find('li.selected-item');
                    list.find('ul').children().remove();
                }

                $$.each(json.results, function (index, value) {
                    if (value.isLastPage) {
                        list.data('lastpage', true);
                    }

                    preprocessData('announcementList', value);
                    var html = Template7.templates.announcementTemplate(value);
                    list.find('ul').append(html);
                });
                if (selectItem) {
                    list.find('ul').find('li[data-id="' + selectItem.data('id') + '"]').addClass('selected-item');
                }
                empty[list.find('ul').children().length > 0 ? 'hide' : 'show']();
            }

            list.data('request', null);
            if (refresing) {
                myApp.pullToRefreshDone();
            }
        });
        list.data('request', req);
    }
    
    ul.on('click', 'a.item-link', function () {
        $$(this).find('.badge').hide();
    });

    $$(page.container).find('.infinite-scroll').on('infinite', function () {
        if (list.data('lastpage') || list.data('request')) {
            return;
        }

        loadPage();
    });
    refreshContainer.on('refresh', function () {
        var request = list.data('request');
        if (request) {
            request.abort();
        }
        list.data('request', null);
        list.data('lastpage', false);
        list.data('nextpage', 0);
        loadPage();
    });
    myApp.pullToRefreshTrigger(refreshContainer);
});

myApp.onPageBeforeInit('announcement-detail', function (page) {
    if (myApp.padApp) {
        $$(page.navbarInnerContainer).find('.left').css('visibility', 'hidden');
    }
});

myApp.onPageAfterBack('announcement-detail', function (page) {
	$$('.page[data-page="announcement-list"] .pull-to-refresh-content').trigger('refresh');
});

myApp.onPageInit('announcement-detail', function (page) {
    console.log("onPageInit " + page.name);
    var params = page.query;

    $$.getJSONP(SERVER_PATH + 'getAnnouncementInfo.do', {pkId: params.pkId}, function (json) {
        if (json.code == 1) {
            var announcement = json.results.announcement[0],
                fujianlist = json.results.fujianlist,
                list1 = $$(page.container).find('.list-block.message-list ul'),
                list2 = $$(page.container).find('.list-block.files-block ul');

            list1.find('.item-title').text(announcement.title);
            //list1.find('.item-subtitle').text('发件人：' + announcement.publishUserName);
            //list1.find('.item-after').text('发布时间：' + announcement.dateStr);
             
            list1.find('.message-text').html(announcement.remark);
            
            var imgSrcNewPath = SCHEME_SERVER_PATH + $(".message-text img").attr("src");
            
            $(".message-text img").attr("src",imgSrcNewPath);
            
            $$.each(fujianlist, function (index, value) {
                value.suffix = value.name.fileSuffix();
                value.suffix = value.suffix || 'file';

                var html = Template7.templates.fileTemplate(value);
                list2.append(html);
            });
            var content = list1.find('.message-content');
            var h = list2.offset().top - content.offset().top - 9 - 5;
            content.css('height', h + 'px');

            $$('.page[data-page="announcement-list"] .pull-to-refresh-content').trigger('refresh');
        }
    });
});